package com.drought.rained.business;

import android.os.Build;
import android.text.TextUtils;

import com.appsflyer.AppsFlyerLib;
import com.drought.rained.BuildConfig;
import com.drought.rained.LJApplication;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.utils.DeviceUtilsLJ;
import com.drought.rained.utils.ScreenUtilsLJ;
import com.drought.rained.utils.UniqueUtilsLJ;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 公共参数生成工厂
 * Created by wangjing on 2017/5/16.
 */
public class ParamsFactoryLJ {

    public static Map<String, String> unChangeCommonParams;
    public static Map<String,String> installCommonParams;

    public static RequestBody getCommonParams(Map<String, Object> formParams) {
        checkUnChangeParams();

        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        params.put("commonParam", unChangeCommonParams);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return FormBody.create(MediaType.parse("application/json"), new Gson().toJson(params));
    }

    private static String getLanguage() {
        if (BuildConfig.DEBUG) {
            return Locale.getDefault().getLanguage();
        } else {
            return "in";
        }
    }

    public static RequestBody getInstallAppParams(Map<String, Object> formParams) {
        checkInstallParams();

        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        params.put("commonParam", unChangeCommonParams);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return FormBody.create(MediaType.parse("application/json"), new Gson().toJson(params));
    }

    public static String getCommonParamsStr(Map<String, Object> formParams) {
        checkUnChangeParams();

        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        params.put("commonParam", unChangeCommonParams);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return new Gson().toJson(params);
    }

    public static RequestBody getParams(Map<String, Object> formParams) {
        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return FormBody.create(MediaType.parse("application/json"), new Gson().toJson(params));
    }

    /**
     * 检查是否给不变的公共参数赋值了
     *
     * @return
     */
    private static void checkUnChangeParams() {
        if (unChangeCommonParams == null || unChangeCommonParams.size() == 0) {
            unChangeCommonParams = new HashMap<>();
            // 客户端软件版本
            unChangeCommonParams.put("cv", DeviceUtilsLJ
                    .getVersionNameAndVersionCode(LJApplication.getContext()));
            // 操作系统
            unChangeCommonParams.put("os", DeviceUtilsLJ.getPlatform());
            // 操作系统版本
            unChangeCommonParams.put("ov", Build.VERSION.RELEASE);
            // 屏幕尺寸
            unChangeCommonParams.put("sc", ScreenUtilsLJ.genScreenResolution());
            // 包名
            unChangeCommonParams.put("pn", LJApplication.getContext().getPackageName());
            // 设备型号
            unChangeCommonParams.put("bd", TextUtils.concat(DeviceUtilsLJ.getBrand(), " ",
                    DeviceUtilsLJ.getModel()).toString());
            unChangeCommonParams.put("osVersion", DeviceUtilsLJ.getSdkVersion());
            unChangeCommonParams.put("lg", getLanguage());
            String media_source = AcacheLJ.get(LJApplication.getContext()).getAsString(AcacheKeysLJ.MEDIA_SOURCE);
            if (!TextUtils.isEmpty(media_source)) {
                unChangeCommonParams.put("ms", media_source);
            }else {
                unChangeCommonParams.put("ms", "organic");
                unChangeCommonParams.put("device_id", UniqueUtilsLJ.getUniquePsuedoID());
                unChangeCommonParams.put("ad", AppsFlyerLib.getInstance().getAppsFlyerUID(LJApplication.getContext()));
            }

        }
    }

    private static void checkInstallParams() {
        if (installCommonParams == null || installCommonParams.size() == 0) {
            installCommonParams = new HashMap<>();
            // 客户端软件版本
            installCommonParams.put("cv", DeviceUtilsLJ
                    .getVersionNameAndVersionCode(LJApplication.getContext()));
            // 操作系统
            installCommonParams.put("os", DeviceUtilsLJ.getPlatform());
            // 操作系统版本
            installCommonParams.put("ov", Build.VERSION.RELEASE);
            // 屏幕尺寸
            installCommonParams.put("sc", ScreenUtilsLJ.genScreenResolution());
            // 包名
            installCommonParams.put("pn", LJApplication.getContext().getPackageName());
            // 设备型号
            installCommonParams.put("bd", TextUtils.concat(DeviceUtilsLJ.getBrand(), " ",
                    DeviceUtilsLJ.getModel()).toString());
            installCommonParams.put("osVersion", DeviceUtilsLJ.getSdkVersion());
            installCommonParams.put("lg", getLanguage());
            String media_source = AcacheLJ.get(LJApplication.getContext()).getAsString(AcacheKeysLJ.MEDIA_SOURCE);
            if (!TextUtils.isEmpty(media_source)) {
                installCommonParams.put("ms", media_source);
            }else {
                installCommonParams.put("ms", "organic");
            }

        }
    }

}
